<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Template_Recurring_Create5 extends Page
{
       public $Label8 = null;
       public $ComboBox2 = null;
       public $Edit2 = null;
       public $Label7 = null;
       public $ComboBox1 = null;
       public $Edit1 = null;
       public $Label6 = null;
       public $DateTimePicker1 = null;
       public $Label4 = null;
       public $CheckBox1 = null;
       public $edDescTemp = null;
       public $lblhours = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $JTJavaScript9 = null;
       public $JTJavaScript8 = null;
       public $JTJavaScript7 = null;
       public $Image1 = null;
       public $imgBack = null;
       public $btnDelDesc = null;
       public $btnAddDesc = null;
       public $edDesc = null;
       public $listDesc = null;
       public $JTDivDesc = null;
       public $JTGroupBox2 = null;
       public $JTDivType = null;
       public $btnDelType = null;
       public $btnAddType = null;
       public $edType = null;
       public $listType = null;
       public $JTGroupBox1 = null;
       public $JTJavaScript6 = null;
       public $JTJavaScript5 = null;
       public $JTJavaScript4 = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $compid = null;
       public $btnShowType = null;
       public $JTSiteTheme1 = null;
       public $btnShowDescription = null;
       public $cmbcompletion = null;
       public $Label3 = null;
       public $lblfrm = null;
       public $dpBasis = null;
       public $hiddenStyle = null;
       public $hiddenId = null;
       public $lblOr = null;
       public $cmbDaysMonths2 = null;
       public $edDueEvery2 = null;
       public $cmbDaysMonths = null;
       public $lblMilesHours = null;
       public $edDueEvery = null;
       public $lblDueEvery = null;
       public $edOthers = null;
       public $lblOther = null;
       public $memoComments = null;
       public $lblComments = null;
       public $cmbVendor = null;
       public $Label2 = null;
       public $edPrice = null;
       public $lblFixedPrice = null;
       public $cmbDesc = null;
       public $Label5 = null;
       public $cmbType = null;
       public $lblType = null;
       public $cmbSchedule = null;
       public $Label1 = null;
       public $lblDueby = null;

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function btnDelDescJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var DescIndex = document.getElementById('listDesc').selectedIndex;
       var DescValue = document.getElementById('listDesc')[DescIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxDesc3(DescValue,comp);
       <?php

       }

       function btnAddDescJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var adddesc = document.getElementById('edDesc').value;
       var comp = document.getElementById('compid').value;
       AjaxDesc2(adddesc,comp);
       document.Template_Recurring_Create.edDesc.value = "";
       <?php

       }

       function btnShowDescriptionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivDesc').Show();
       document.Template_Recurring_Create.edDesc.value = "";
       <?php

       }

       function btnDelTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var TypeIndex = document.getElementById('listType').selectedIndex;
       var TypeValue = document.getElementById('listType')[TypeIndex].value;
       var comp = document.getElementById('compid').value;

       var complete =document.getElementById('cmbcompletion').value

       if(complete=="WorkOrder")
       {
       AjaxType3(TypeValue,comp);
       }

       if(complete=="Payment")
       {
       AjaxTemp3(TypeValue,comp);
       }

       if(complete=="None")
       {
       AjaxTemp3(TypeValue,comp);
       }
       <?php

       }

       function btnAddTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addtype = document.getElementById('edType').value;
       var comp = document.getElementById('compid').value;

       var completion=document.getElementById('cmbcompletion').value

       if(completion=="WorkOrder")
       {
       AjaxType2(addtype,comp);
       document.Template_Recurring_Create.edType.value = "";
       }

       if(completion=="Payment")
       {
       AjaxTemp2(addtype,comp);
       document.Template_Recurring_Create.edType.value = "";
       }

       if(completion=="None")
       {
       AjaxTemp2(addtype,comp);
       document.Template_Recurring_Create.edType.value = "";
       }
       <?php

       }



       function btnShowTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivType').Show();
       document.Template_Recurring_Create.edType.value = "";
       <?php

       }


       function cmbcompletionJSChange($sender, $params)
       {

      echo $this->cmbcompletion->ajaxCall('CompletionChange');
       ?>
       //Add your javascript code here
       return false;
       <?php


       }

       function btnCancelClick($sender, $params)
       {
       redirect('template_recurring_list.php?template_id='.$this->hiddenId->getValue().'&style='.$this->hiddenStyle->getValue());


       }


       function CompletionChange()
       {
       $completion = $this->cmbcompletion->getItemIndex();

       if($completion=="WorkOrder")
       {
        $this->cmbType->Clear();
        $result_type = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='type'");

          while($row_type = mysql_fetch_array($result_type))
          {
            $this->cmbType->AddItem($row_type['value'], null, $row_type['value']);
          }

        $this->listType->Clear();
        $result_t = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='type'");

          while($row_t = mysql_fetch_array($result_t))
          {
            $this->listType->AddItem($row_t['value'], null, $row_t['value']);
          }

       }


       if($completion=="Payment")
       {
        $this->cmbType->Clear();
        $result_type = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

          while($row_type = mysql_fetch_array($result_type))
          {
            $this->cmbType->AddItem($row_type['value'], null, $row_type['value']);
          }

        $this->listType->Clear();
        $result_t = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

          while($row_t = mysql_fetch_array($result_t))
          {
            $this->listType->AddItem($row_t['value'], null, $row_t['value']);
          }

       }

       if($completion=="None")
       {
        $this->cmbType->Clear();
        $result_type = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

          while($row_type = mysql_fetch_array($result_type))
          {
            $this->cmbType->AddItem($row_type['value'], null, $row_type['value']);
          }

        $this->listType->Clear();
        $result_t = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

          while($row_t = mysql_fetch_array($result_t))
          {
            $this->listType->AddItem($row_t['value'], null, $row_t['value']);
          }

       }


       }



       function Template_Recurring_Create5Create($sender, $params)
       {
       $this->compid->Value = $_SESSION['company_id'];
       $template = $_GET['template'];
       $style = $_GET['style'];


       $this->cmbSchedule->ItemIndex='Usage';
       $this->dpBasis->Visible=false;
       $this->lblfrm->Visible=false;

       $this->hiddenId->setValue($template);
       if($style!=null || $style!='')
       $this->hiddenStyle->setValue($style);


       //$this->cmbType->ClearSelection();
       /*//completion work order
       if($this->cmbcompletion->ItemIndex = "Work Order")
       {
       $this->cmbType->Clear();
       $result_type = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='type'");

        while($row_type = mysql_fetch_array($result_type))
        {
            $this->cmbType->AddItem($row_type['value'], null, $row_type['value']);
        }

       //list type
       $this->listType->Clear();
       $result_t = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='type'");

        while($row_t = mysql_fetch_array($result_t))
        {
            $this->listType->AddItem($row_t['value'], null, $row_t['value']);
        }

        }

       //completion payment
       if($this->cmbcompletion->ItemIndex = "Payment")
       {
       $this->cmbType->Clear();
       $result_type = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

        while($row_type = mysql_fetch_array($result_type))
        {
            $this->cmbType->AddItem($row_type['value'], null, $row_type['value']);
        }

       //list type
       $this->listType->Clear();
       $result_t = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

        while($row_t = mysql_fetch_array($result_t))
        {
            $this->listType->AddItem($row_t['value'], null, $row_t['value']);
        }

        } */


       $this->cmbDesc->Clear();
       $result_desc = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='task descriptions'");

        while($row_desc = mysql_fetch_array($result_desc))
        {
            $this->cmbDesc->AddItem($row_desc['value'], null, $row_desc['value']);
        }

        // list description
        $this->listDesc->Clear();
         $result_d = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='task descriptions'");

        while($row_d = mysql_fetch_array($result_d))
        {
            $this->listDesc->AddItem($row_d['value'], null, $row_d['value']);
        }

       $this->cmbVendor->Clear();
       $result_vendor = mysql_query("select vendor_name from vendor_tbl where company_id='".$_SESSION['company_id']."'");

        while($row_vendor = mysql_fetch_array($result_vendor))
        {
            $this->cmbVendor->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
        }

       $this->edDueEvery2->text='';
       $this->edDueEvery->text='';
       $this->edOthers->Text='';
       $this->edPrice->Text='';

       $this->imgBack->setLink('template_recurring_list.php?template_id='.$this->hiddenId->getValue().'&style='.$this->hiddenStyle->getValue());
       }



       function btnSubmitClick($sender, $params)
       {
       $time_save = null;
       $miles_save = null;
       $schedBy_save = null;
       if($this->cmbSchedule->getItemIndex()=='Time')
       {
       $time_save = $this->edDueEvery->Text;
       $schedBy_save = $this->cmbDaysMonths->getItemIndex();
       }
       if($this->cmbSchedule->getItemIndex()=='Both')
       {
       $miles_save = $this->edDueEvery->Text;
       $time_save = $this->edDueEvery2->Text;
       $schedBy_save = $this->cmbDaysMonths2->getItemIndex();
       }
       if($this->cmbSchedule->getItemIndex()=='Usage')
       {
       $miles_save = $this->edDueEvery->Text;
       }


        //recurring time
       if($this->cmbSchedule->getItemIndex()=='Time')
       {
       $val = $this->edDueEvery->Text;
       $days_mos = $this->cmbDaysMonths->getItemIndex();
       }

       if($this->cmbSchedule->getItemIndex()=='Both')
       {
       $val = $this->edDueEvery2->Text;
       $days_mos = $this->cmbDaysMonths2->getItemIndex();
       }


       $basis = $this->dpBasis->Text;

       if($this->cmbSchedule->getItemIndex()!='Usage')
       {

       if($days_mos=='Days')
       {
       $date = date($basis);
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." day");
       $due_date = date("Y-m-d", $date_computed);
       }

       if($days_mos=='Months')
       {
       $date = date($basis);
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." month");
       $due_date = date("Y-m-d", $date_computed);
       }

       }




       $result_get_style = mysql_query("SELECT template_type FROM template_tbl WHERE template_id='".$this->hiddenId->getValue()."'");
       $row_style = mysql_fetch_array($result_get_style);

       $style_id = $this->hiddenStyle->getValue();
       $result_style = mysql_query("SELECT value FROM lookup_vehicle_tbl WHERE id='".$style_id."'") or die("ERROR STYLE ".mysql_error());
       $row_style = mysql_fetch_array($result_style);
       $style_name = $row_style['value'];



       $result_reminder=mysql_query("insert into template_recurring_tbl (company_id,
       template_id,
       template_style,
       style_id,
       schedule_by,
       due_usage,
       due_time,
       due_by,
       date_basis,
       computed_date,
       completion_window,
       type,
       description,
       other_description,
       price,
       vendor,
       comments)

       values('".$_SESSION['company_id']."',
       '".$this->hiddenId->getValue()."',
       '".$style_name."',
       '".$this->hiddenStyle->getValue()."',
       '".$this->cmbSchedule->getItemIndex()."',
       '".$miles_save."',
       '".$time_save."',
       '".$schedBy_save."',
       '".$this->dpBasis->Text."',
       '".$due_date."',
       '".$this->cmbcompletion->getItemIndex()."',
       '".$this->cmbType->getItemIndex()."',
       '".$this->edDescTemp->Text."',
       '".$this->edOthers->Text."',
       '".$this->edPrice->Text."',
       '".$this->cmbVendor->getItemIndex()."',
       '".$this->memoComments->Text."')")or die("INSERT RECURRING_TASK - " . mysql_error());

       redirect('template_recurring_list.php?template_id='.$this->hiddenId->getValue().'&style='.$this->hiddenStyle->getValue());
       }

       function cmbScheduleChange($sender, $params)
       {
       $schedule = $this->cmbSchedule->getItemIndex();

       if($schedule=='Time')
       {
       $this->edDueEvery2->Visible=false;
       $this->cmbDaysMonths2->Visible=false;
       $this->lblMilesHours->Visible=false;
       $this->lblOr->Visible=false;
       $this->cmbDaysMonths->Visible=true;
       $this->edDueEvery->Visible=true;
       $this->dpBasis->Visible=true;
       $this->dpBasis->Left=445;
       $this->lblfrm->Left=409;
       $this->lblfrm->Visible=true;
       $this->lblhours->Visible=false;

       }
       else if ($schedule=='Both')
       {
       $this->cmbDaysMonths->Visible=false;
       $this->edDueEvery2->Visible=true;
       $this->cmbDaysMonths2->Visible=true;
       $this->lblMilesHours->Visible=true;
       $this->lblOr->Visible=true;
       $this->dpBasis->Visible=true;
       $this->dpBasis->Left=612;
       $this->lblfrm->Visible=true;
       $this->lblfrm->Left=570;
       }
       else if ($schedule=='Usage')
       {
       $this->edDueEvery->Visible=true;
       $this->lblMilesHours->Visible=true;
       $this->edDueEvery2->Visible=false;
       $this->cmbDaysMonths2->Visible=false;
       $this->cmbDaysMonths->Visible=false;
       $this->lblOr->Visible=false;
       $this->dpBasis->Visible=false;
       $this->lblfrm->Visible=false;
       $this->lblhours->Visible=false;
       }
       else if ($schedule=='Hours')
       {
       $this->edDueEvery->Visible=true;
       $this->lblMilesHours->Visible=false;
       $this->edDueEvery2->Visible=false;
       $this->cmbDaysMonths2->Visible=false;
       $this->cmbDaysMonths->Visible=false;
       $this->lblOr->Visible=false;
       $this->dpBasis->Visible=false;
       $this->lblfrm->Visible=false;
       $this->lblhours->Visible=true;
       }


       }

}

global $application;

global $Template_Recurring_Create5;

//Creates the form
$Template_Recurring_Create5=new Template_Recurring_Create5($application);

//Read from resource file
$Template_Recurring_Create5->loadResource(__FILE__);

//Shows the form
$Template_Recurring_Create5->show();

?>